/**
 *  (c)
 * <p>
 *
 * <p>
 *
 */

package com.nn.smart.bus.controller.v2;

import com.nn.smart.bus.commons.api.version.ApiVersion;
import com.nn.smart.bus.commons.tools.constant.Constant;
import com.nn.smart.bus.commons.tools.utils.Result;
import com.nn.smart.bus.commons.tools.validator.ValidatorUtils;
import com.nn.smart.bus.commons.tools.validator.group.AddGroup;
import com.nn.smart.bus.commons.tools.validator.group.DefaultGroup;
import com.nn.smart.bus.commons.tools.validator.group.UpdateGroup;
import com.nn.smart.bus.dto.SysRoleDTO;
import com.nn.smart.bus.enums.AppMenuCategoryEnum;
import com.nn.smart.bus.service.SysRoleDataScopeService;
import com.nn.smart.bus.service.SysRoleMenuService;
import com.nn.smart.bus.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 角色管理
 */
@ApiVersion(2)
@RestController
@RequestMapping("role" + Constant.VERSION_CONTROL)
public class SysRoleV2Controller {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysRoleDataScopeService sysRoleDataScopeService;

    @GetMapping("{id}")
    public Result<SysRoleDTO> get(@PathVariable("id") Long id) {
        SysRoleDTO data = sysRoleService.get(id);

        //查询角色对应的菜单
        data.setMenuIdList(sysRoleMenuService.getMenuIdList(id));
        //查询角色对应的数据权限
        data.setDeptIdList(sysRoleDataScopeService.getDeptIdList(id));

        return new Result<SysRoleDTO>().ok(data);
    }


    @PostMapping
    public Result save(@RequestBody SysRoleDTO dto) {
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        sysRoleService.saveV2(dto);
        return new Result();
    }

    @PutMapping
    public Result update(@RequestBody SysRoleDTO dto) {
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        sysRoleService.updateV2(dto);
        return new Result();
    }

}
